home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Imaging / RealShapes / RectShpe.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  4.8 KB  |  279 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        RectShpe.h
  3.  
  4.     Contains:    RealShape class, private to ODShape.
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.          <7>     9/13/95    DM        1277216 GM:API return no ODPoints nor
  13.                                     ODPolygons
  14.          <6>     8/16/95    NP        1274946: ErrorDef.idl problems. Add include
  15.                                     file.
  16.          <5>     5/25/95    jpa        Use new GX headers [1241078, 1253324]
  17.          <4>     12/5/94    jpa        gxSolidFill -> gxWindingFill. [1191192]
  18.          <3>      8/8/94    jpa        Added Outset method [1178690]
  19.          <2>      8/2/94    jpa        Use AsPolygonShape, not Promote, in
  20.                                     Transform method.
  21.          <1>     6/15/94    jpa        first checked in
  22.  
  23.     In Progress:
  24. */
  25.  
  26.  
  27. #ifndef _ALTPOINT_
  28. #include "AltPoint.h"            /* Use C++ savvy XMPPoint and XMPRect*/
  29. #endif
  30.  
  31. #ifndef _ALTPOLY_
  32. #include "AltPoly.h"
  33. #endif
  34.  
  35. #ifndef _RECTSHPE_
  36. #include "RectShpe.h"
  37. #endif
  38.  
  39. #ifndef _PolySHPE_
  40. #include "PolyShpe.h"
  41. #endif
  42.  
  43. #ifndef SOM_ODTransform_xh
  44. #include "Trnsform.xh"
  45. #endif
  46.  
  47. #ifndef _EXCEPT_
  48. #include "Except.h"
  49. #endif
  50.  
  51. #ifndef _UTILERRS_
  52. #include "UtilErrs.h"
  53. #endif
  54.  
  55. #ifndef __GXGRAPHICS__
  56. #include <GXGraphics.h>
  57. #endif
  58.  
  59.  
  60. #pragma segment ODShape
  61.  
  62.  
  63. RectShape::RectShape( ODGeometryMode mode, const ODRect &rect )
  64.     :RealShape(mode),
  65.      fRect(rect)
  66. {
  67. #if ODDebug
  68.     fType = 0;
  69. #endif
  70. }
  71.  
  72.  
  73. void
  74. RectShape::GetBoundingBox( ODRect *bounds )
  75. {
  76.     *bounds = fRect;
  77. }
  78.  
  79.  
  80. RealShape*
  81. RectShape::SetRectangle( const ODRect *r )
  82. {
  83.     fRect = *r;
  84.     this->Purge(0);
  85.     return this;
  86. }
  87.  
  88.  
  89. void
  90. RectShape::CopyPolygon( ODPolygon &poly )
  91. {
  92.     poly.SetRect(fRect);
  93. }
  94.  
  95.  
  96. void
  97. RectShape::InitQDRegion( )
  98. {
  99.     Rect r;
  100.     fRect.AsQDRect(r);
  101.     RectRgn(fQDRegion,&r);
  102. }
  103.  
  104.  
  105. gxShape
  106. RectShape::CopyGXShape( )
  107. {
  108.     ASSERT(gGX>0,kODErrAssertionFailed);
  109.     gxShape r = GXNewRectangle( (gxRectangle*) &fRect );
  110.     GXSetShapeFill(r,gxWindingFill);
  111.     ThrowIfGXError();
  112.     return r;
  113. }
  114.  
  115.  
  116. ODBoolean
  117. RectShape::IsSameAs( RealShape *shape )
  118. {
  119.     if( !shape->IsRectangular() )
  120.         return kODFalse;
  121.     ODRect r;
  122.     shape->GetBoundingBox(&r);
  123.     return r == fRect;
  124. }
  125.  
  126.  
  127. ODBoolean
  128. RectShape::IsEmpty( )
  129. {
  130.     return fRect.IsEmpty();
  131. }
  132.  
  133.  
  134. ODBoolean
  135. RectShape::ContainsPoint( ODPoint point )
  136. {
  137.     return fRect.Contains(point);
  138. }
  139.  
  140.  
  141. ODBoolean
  142. RectShape::IsRectangular( )
  143. {
  144.     return kODTrue;        // Duhhhhhh...
  145. }
  146.  
  147.  
  148. RealShape*
  149. RectShape::Clear( )
  150. {
  151.     fRect.Clear();
  152.     this->Purge(0);
  153.     return this;
  154. }
  155.  
  156.  
  157. RealShape*
  158. RectShape::Copy( )
  159. {
  160.     RectShape *r = new RectShape(fMode,fRect);
  161.     return r->SetRectangle(&fRect);
  162. }
  163.  
  164.  
  165. RealShape*
  166. RectShape::Transform( Environment *ev, ODTransform *xform )
  167. {
  168.     if( fRect.IsEmpty() )
  169.         ;                                    // I'm empty: do nothing
  170.         
  171.     else if( xform->GetType(ev) <= kODScaleTranslateXform ) {
  172.         ODPoint topLeft = fRect.TopLeft();
  173.         ODPoint botRight = fRect.BotRight();
  174.         
  175.         xform->TransformPoint(ev,&topLeft);
  176.         xform->TransformPoint(ev,&botRight);
  177.         
  178.         fRect = ODRect(topLeft, botRight);
  179.         this->Purge(0);
  180.         
  181.     } else {
  182.         RealShape* s = this->AsPolygonShape();    // General case: Promote myself
  183.         TRY{
  184.             s= s->Transform(ev,xform);    // ...and ask the promoted shape to do the job.
  185.         }CATCH_ALL{
  186.             delete s;
  187.             RERAISE;
  188.         }ENDTRY
  189.         delete this;
  190.         return s;
  191.     }
  192.     
  193.     return this;
  194. }
  195.  
  196.  
  197. RealShape*
  198. RectShape::Outset( ODCoordinate distance )
  199. {
  200.     if( ! fRect.IsEmpty() ) {
  201.         fRect.Inset(-distance,-distance);
  202.         this->Purge(0);
  203.     }
  204.     return this;
  205. }
  206.  
  207.  
  208. RealShape*
  209. RectShape::Subtract( RealShape *shape )
  210. {
  211.     if( shape->IsEmpty() || this->IsEmpty() )                // I (heart) no-ops
  212.         return this;
  213.         
  214.     else if( shape==this )                                    // S - S = 0
  215.         return this->Clear();
  216.  
  217.     else {
  218.         ODRect itsBounds;
  219.         shape->GetBoundingBox(&itsBounds);
  220.         if( !fRect.Intersects(itsBounds) )
  221.             return this;
  222.         else
  223.             return this->Promote(kShapeDifference,shape);
  224.     }
  225. }
  226.  
  227.  
  228. RealShape*
  229. RectShape::Intersect( RealShape *shape )
  230. {
  231.     ODRect r;
  232.     
  233.     if( shape==this )
  234.         return this;
  235.         
  236.     shape->GetBoundingBox(&r);
  237.     
  238.     if( shape->IsRectangular() ) {
  239.         fRect &= r;                            // Intersection of two rects is a rect
  240.         this->Purge(0);
  241.     
  242.     } else if( this->IsEmpty() || shape->IsEmpty() || !fRect.Intersects(r) ) {
  243.         fRect.Clear();                        // One of us is empty, or we don't intersect
  244.         this->Purge(0);                        // so make myself empty
  245.     
  246.     } else if( fRect.Contains(r) )            // I contain the shape, so make myself a copy of it
  247.         return this->ReplaceWith(shape);
  248.     
  249.     else
  250.         return this->Promote(kShapeIntersection,shape);
  251.     
  252.     return this;
  253. }
  254.  
  255.  
  256. RealShape*
  257. RectShape::Union( RealShape *shape )
  258. {
  259.     if( shape==this || shape->IsEmpty() )    // Shape is me or is empty: no-op
  260.         return this;
  261.     
  262.     if( this->IsEmpty() )                    // I am empty: change to a copy of the other shape
  263.         return this->ReplaceWith(shape);
  264.         
  265.     ODRect bounds;
  266.     shape->GetBoundingBox(&bounds);
  267.     if( fRect.Contains(bounds) )
  268.         ;                                    // Shape is contained in me: no-op
  269.     
  270.     else if( shape->IsRectangular() && bounds.Contains(fRect) ) {
  271.         fRect = bounds;
  272.         this->Purge(0);                        // I am contained in another rectangle:
  273.                                             // just grow my bounds to its
  274.     } else
  275.         return this->Promote(kShapeUnion,shape);
  276.     
  277.     return this;
  278. }
  279.